package tss.clientGui;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class SoundRunner extends Thread {
	
	private SoundTypes soundType = null;
	
	public SoundRunner(SoundTypes soundType) {
		this.soundType = soundType;
	}
	
	public SoundTypes getSoundType() {
		return soundType;
	}
	
	public void run() {
		Clip clip = null;
		try {
			File file = null;
			
			if (soundType == SoundTypes.Ring) {
				file = new File("src//tss//resources//ring3.wav");
			} else if (soundType == SoundTypes.Busy) {
				file = new File("src//tss//resources//phonebz.wav");
			} else if (soundType == SoundTypes.Dial) {
				file = new File("src//tss//resources//dialtone.wav");
			} else if (soundType == SoundTypes.Unavailable) {
				file = new File("src//tss//resources//notcomplete.wav");
			} else {
				return;
			}
			
		
			clip = AudioSystem.getClip();
			clip.open(AudioSystem.getAudioInputStream(file));
			
			while(true && !Thread.interrupted()) {
				clip.start();
				clip.setFramePosition(0);
				while(clip.isRunning() && !Thread.interrupted())
					Thread.sleep(100);
				Thread.sleep(1000);
			}
			//clip.close();
		
		} catch (LineUnavailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally { 
			if (clip != null)
				clip.close();
		}
	}
	

}
